package com.bishe.app.modules.userSaleIncome.model;

import cn.hutool.core.lang.Pair;
import cn.hutool.core.util.ReflectUtil;
import com.bishe.app.core.common.BizException;
import com.bishe.app.core.enums.IsDel;
import com.bishe.app.core.utils.AppRunnerUtils;
import com.bishe.app.modules.group.entity.Group;
import com.bishe.app.modules.group.service.GroupService;
import com.bishe.app.modules.user.entity.User;
import com.bishe.app.modules.user.service.UserService;
import jdk.nashorn.internal.ir.Optimistic;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Optional;

/**
 * Created : 2024/6/25, 17:10
 *
 * @author : jeffrey.hu
 **/
@Data
@NoArgsConstructor
public class UserSalaryModel extends UserSaleIncomeDetailModel {
    private Long userId;
    private Long groupId;
    private String name;

    public UserSalaryModel(Long userId, Long groupId, UserSaleIncomeDetailModel detailModel) {
        super(detailModel);
        this.userId = userId;
        this.groupId = groupId;
    }

    public String[][] genImgData() {
        User user = AppRunnerUtils.getBean(UserService.class).lambdaQuery()
                .eq(User::getId, this.userId)
                .eq(User::getIsDel, IsDel.N.getCode())
                .one();
        if (user == null) {
            throw new BizException("不存在的用户.请联系管理员");
        }

        Group group = AppRunnerUtils.getBean(GroupService.class).lambdaQuery().eq(Group::getId, this.groupId).one();
        if (group == null) {
            throw new BizException("不存在的店铺.请联系管理员");
        }

        this.name = user.getName();

        List<Pair<String, String>> groupKvs = UserIncomeKV.getGroupKvs(group.getType());

        String[][] tableContent = new String[2][groupKvs.size() + 1];
        tableContent[0][0] = "姓名";
        tableContent[1][0] = this.name;

        for (int i = 0; i < groupKvs.size(); i++) {
            tableContent[0][i + 1] = groupKvs.get(i).getKey();
            tableContent[1][i + 1] = Optional.ofNullable(ReflectUtil.getFieldValue(this, groupKvs.get(i).getValue())).orElse("0").toString();
        }

        return tableContent;
    }
}
